home *** CD-ROM | disk | FTP | other *** search
/ 9-Digit Zip Code Directory / 9-Digit Zip Code Directory (American Business Information) (ABIZIP-12).ISO / z4src.zip / Z4PFWRK.C < prev    next >
C/C++ Source or Header  |  1995-07-29  |  7KB  |  256 lines

  1. //----------------------------------------------------------------------------
  2. //                            MODULE DESCRIPTION
  3. //
  4. //  Module:    z4pfwrk.c
  5. //   Title:    9-Digit ZIP Code Directory -- on CD-ROM
  6. //  Notice:    John M. Weeder
  7. //                 Copyright (c) 1993. All rights reserved.
  8. //             This module contains proprietary information and should be 
  9. //                treated as confidential.
  10. //
  11. //----------------------------------------------------------------------------
  12. //                           MAINTENANCE HISTORY
  13. //
  14. // $Workfile$
  15. // $Revision$
  16. //   $Author$
  17. //     $Date$
  18. //      $Log$    
  19. //
  20. //----------------------------------------------------------------------------
  21. //                             MODULE NARRATIVE
  22. //
  23. //    This module contains the program entry point for
  24. //
  25. //    The code in this module may be written in C++ or C.
  26. //
  27. //    This module is portable to:
  28. //        DOS 3.X+
  29. //        MS Windows 3.X+
  30. //        OS/2 2.X+
  31. //        OS/2 2.0 PM
  32. //
  33. //    The following compilers are supported:
  34. //        MSC 6.0A
  35. //        MSC/C++ 7.0
  36. //        Borland C++ 3.1 for DOS
  37. //        Borland C++ 1.0 for OS/2 2.X
  38. //
  39. //----------------------------------------------------------------------------
  40. #include <z4.h>
  41.  
  42.  
  43. //----------------------------------------------------------------------------
  44. //    Stack size
  45. //----------------------------------------------------------------------------
  46. #if COMPILER_BORLAND && (OS_DOS || OS_WINDOWS)
  47. unsigned _stklen = 0x4000;
  48. #endif
  49.  
  50.  
  51. //----------------------------------------------------------------------------
  52. //    Globals
  53. //----------------------------------------------------------------------------
  54.  
  55. //
  56. // A useless string which is simply embedded in the executable.
  57. //
  58. PSZ __pszCredits__ = "Written by John M Weeder, 1993";
  59.  
  60.  
  61. //
  62. //    This should contain a program description which will be displayed as
  63. //    part of the program's help text.
  64. //
  65. static PCSZ pcszDescription =
  66.     "This program generates the batch and script files used for the file\n"
  67.     "build.";
  68.  
  69. //
  70. //    Program command line options
  71. //
  72. static BS_CMDOPT acmdopt[] =
  73.     {
  74.     BS_CMDOPT_NULL,
  75.     };
  76.  
  77.  
  78. //----------------------------------------------------------------------------
  79. //   Description:    main() - Program entry point
  80. //    Parameters:    Standard C parameters
  81. //       Returns:    DOS return code.
  82. //----------------------------------------------------------------------------
  83. int main(int argc, char **argv)
  84. {
  85. static BS_CFG cfg = CFG_DFT;
  86.     int retval = 99;
  87.     CHAR szName[MAX_PATH];
  88.     SIZET cb;
  89.     PBYTE pb = NULL;
  90.     FLAG16 fs;
  91.     LONG lSize;
  92.     PBYTE pbRec;
  93.     HF hf = -1;
  94.     HF hfDos = -1;
  95.     HF hfUnix = -1;
  96.     BOOL fFirst = TRUE;
  97.     SIZET cRec = 0;
  98.     SIZET cScript = 0;
  99.     CHAR szZip3[4], szFinance[7];
  100.     CHAR szFormat[160];
  101.  
  102.     //
  103.     //    Initialize base library
  104.     //
  105.     BaseLibraryInitialize(argc, argv, &cfg);
  106.     BaseTitle("$Revision:  93.1  $", __DATE__, __TIME__, "9-Digit ZIP Disc Batch/Script File Build");
  107.     if (!BaseTitleHelp(acmdopt, pcszDescription,NULL))
  108.         return 99;
  109.     //
  110.     //    Read entire PF file
  111.     //
  112.     strcpy(szName, "z4pf.inp");            // Open file
  113.     FnameQualify(szName, NULL, EnvGet("DATA"), 0);
  114.     fs = FL_OPEN|FL_READONLY|FL_BINARY;
  115.     if (!FileOpen(&hf, szName, fs, NULL))
  116.         goto ERROR_EXIT;
  117.  
  118.     lSize = FileGetSize(hf);                // Get size
  119.     if (lSize <= 0 || lSize > 30000)
  120.         goto ERROR_EXIT;
  121.  
  122.     cb = (SIZET)lSize;                        // Allocate buffer
  123.     pb = MemAlloc(cb + 1);
  124.     if (pb == NULL)
  125.         goto ERROR_EXIT;
  126.  
  127.     if (!FileRead(hf, pb, cb, 0L))           // Read file
  128.         goto ERROR_EXIT;
  129.     pb[cb] = '\0';                                  // Add a null terminator!
  130.  
  131.     FileClose(hf);                                // Close file
  132.     hf = -1;
  133.  
  134.     //
  135.     //    Process POF file
  136.     //
  137.     pbRec = pb;
  138.     szFinance[0] = '\0';
  139.     while (*pbRec)
  140.         {
  141.         //
  142.         //    Open new input files if needed!!
  143.         //
  144.         if ((fFirst && cRec == 0)
  145.         || (fFirst && cRec == 25)
  146.         || (!fFirst && cRec == 50))
  147.             {
  148.             cScript++;
  149.  
  150.           if (hfDos >= 0)                    // Close existing files
  151.                 {
  152.               FileClose(hfDos);
  153.                 hfDos = -1;
  154.                 }
  155.           if (hfUnix >= 0)
  156.                 {
  157.               sprintf(szFormat,
  158.                     "\r\n"
  159.                     "z4pfutil /work /split /u=%s\r\n"
  160.                     "z4z4bld /compress\r\n",
  161.                   szFinance);
  162.       
  163.               if (!FileWrite(hfUnix, szFormat, strlen(szFormat), -1L))
  164.                   goto ERROR_EXIT;
  165.                 
  166.               FileClose(hfUnix);
  167.                 hfUnix = -1;
  168.                 }
  169.                                                     // Open DOS batch file
  170.             sprintf(szName, "DOS%u.BAT", cScript);
  171.             FnameQualify(szName, "BAT", EnvGet("DATA"), 0);
  172.             fs = FL_CREATE|FL_TRUNCATE|FL_READWRITE|FL_DENYREADWRITE|FL_BINARY;
  173.             if (!FileOpen(&hfDos, szName, fs, NULL))
  174.                 goto ERROR_EXIT;
  175.                                                     // Open unix script
  176.             sprintf(szName, "unix%u", cScript);
  177.             FnameQualify(szName, NULL, EnvGet("DATA"), 0);
  178.             fs = FL_CREATE|FL_TRUNCATE|FL_READWRITE|FL_DENYREADWRITE|FL_BINARY;
  179.             if (!FileOpen(&hfUnix, szName, fs, NULL))
  180.                 goto ERROR_EXIT;
  181.  
  182.             if (cRec)
  183.                 {
  184.                fFirst = FALSE;
  185.                cRec = 0;
  186.                 }
  187.             }
  188.         cRec++;
  189.  
  190.         //
  191.         //    Extract data from record
  192.         //
  193.         memcpy(szZip3, pbRec, 3);
  194.         szZip3[3] = '\0';
  195.         memcpy(szFinance, pbRec + 4, 6);
  196.         szFinance[6] = '\0';
  197.  
  198.         if (strcmp(szZip3, "901") == 0)    // No data for this ZIP3
  199.             goto SKIP_RECORD;
  200.  
  201.         //
  202.         // Output data
  203.         //
  204.         sprintf(szFormat,
  205.             "pkunzip -n f:\\zip4\\%s\r\n"
  206.             "if not exist %s.txt (echo %s.txt not created ^ echo %s.txt not created >> c:\\zip4\\error ^ quit)\r\n"
  207.             "\r\n",
  208.             szZip3, szZip3, szZip3, szZip3);
  209.  
  210.         if (!FileWrite(hfDos, szFormat, strlen(szFormat), -1L))
  211.             goto ERROR_EXIT;
  212.  
  213.         sprintf(szFormat,
  214.             "z4pfsplt /delete %s.txt\r\n",
  215.             szZip3);
  216.  
  217.         if (!FileWrite(hfUnix, szFormat, strlen(szFormat), -1L))
  218.             goto ERROR_EXIT;
  219.  
  220.         //
  221.         //    Move to start of next record
  222.         //
  223. SKIP_RECORD:
  224.         while (*pbRec && *pbRec != '\n')
  225.             pbRec++;
  226.  
  227.         while (*pbRec && (*pbRec == '\n' || *pbRec == '\r' || *pbRec == '\x1A'))
  228.             pbRec++;
  229.         }
  230.     if (hfUnix >= 0)
  231.         {
  232.        strcpy(szFormat,
  233.             "\r\n"
  234.             "z4pfutil /work /split /p=30000\r\n"
  235.             "z4z4bld /compress\r\n");
  236.    
  237.        if (!FileWrite(hfUnix, szFormat, strlen(szFormat), -1L))
  238.           goto ERROR_EXIT;
  239.         }
  240.  
  241.     retval = 0;
  242. ERROR_EXIT:
  243.     if (pb)
  244.         MemFree(pb);
  245.     if (hf >= 0)
  246.         FileClose(hf);
  247.     if (hfDos >= 0)
  248.         FileClose(hfDos);
  249.     if (hfUnix >= 0)
  250.         FileClose(hfUnix);
  251.     return retval;
  252. }
  253. //----------------------------------------------------------------------------
  254. //------------------------------- End of File --------------------------------
  255. //----------------------------------------------------------------------------
  256.